package com.iwomy.envpro.platform.utils;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.admin.api.entity.SysDept;
import com.iwomy.envpro.admin.api.feign.RemoteDeptService;
import com.iwomy.envpro.common.core.util.SpringContextHolder;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

public class DeptFillUtils {
	private static RemoteDeptService remoteDeptService = SpringContextHolder.getBean(RemoteDeptService.class);

	//填充部门名称
	public static void fill(Object o, Map<Long, SysDept> map) {

		try {
			Class clazz = o.getClass();
			Method getDeptId = clazz.getMethod("getDeptId");
			Long deptId = (Long) getDeptId.invoke(o);
			Method setDeptName = clazz.getMethod("setDeptName", String.class);
			SysDept sysDept = map.get(deptId);
			setDeptName.invoke(o, ObjectUtil.isNull(sysDept) ? null : sysDept.getName());
		} catch (Exception e) {
			return;
		}
	}

	public static void fill(List list) {
		Map<Long, SysDept> data = remoteDeptService.getAllMapByTenantId().getData();
		for (Object o : list) {
			fill(o, data);
		}
	}

	public static void fill(Page page) {
		Map<Long, SysDept> data = remoteDeptService.getAllMapByTenantId().getData();
		List records = page.getRecords();
		fill(records);
	}
}
